package com.tangkun.environment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.gongzuo.CheckBoxBeanRadio;
import com.tangkun.environment.entity.gongzuo.EnterpriseDetailReturn;
import com.tangkun.environment.utils.GsonUtil;
import com.tangkun.environment.utils.LogUtils;
import com.tangkun.environment.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: BianJiZILiaoAdapter
 * @Description: 编辑资料列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class BianJiZILiaoAdapter extends BaseAdapter {

    private Context context;
    private List<EnterpriseDetailReturn.DataBean.BasiclistBean> dataList = new ArrayList<EnterpriseDetailReturn.DataBean.BasiclistBean>();

    public BianJiZILiaoAdapter(Context context, List<EnterpriseDetailReturn.DataBean.BasiclistBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_tijiaoxunchashuju, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //模块A 的flag=1时 遍历整个数据 查询类型是radio的，查询选项中  parentid等于A的ids并此value=1则显示  flag不等于1的都显示
            if (!TextUtils.isEmpty(dataList.get(position).flag) && dataList.get(position).flag.equals("1")) {
                //flag=1时 遍历整个数据 查询类型是radio的，查询选项中  parentid等于A的ids并且此value=1则显示
                //是否选中了有
                boolean isYesChecked = false;
                for (EnterpriseDetailReturn.DataBean.BasiclistBean bean : dataList) {
                    for (EnterpriseDetailReturn.DataBean.BasiclistBean.ItemlistBean itemBean : bean.itemlist) {
                        //遍历整个数据 查询类型是radio的，查询选项中  parentid等于A的ids
                        if (itemBean.type.equals("radio")) {
                            //并且此value=1则显示
                            if (!TextUtils.isEmpty(itemBean.value)) {
                                //并且他的values里面的childids和ids相等，并且其values为1，就显示当前item，否则隐藏该item
                                String jsonString = itemBean.value;
                                jsonString = jsonString.replaceAll("\\\\", "");
                                List<CheckBoxBeanRadio> checkBoxBeanList = GsonUtil.GsonToList(jsonString, CheckBoxBeanRadio.class);
                                for (int i = 0; i < checkBoxBeanList.size(); i++) {
                                    //查询选项中  parentid等于A的ids 并此value=1则显示
                                    if (!TextUtils.isEmpty(checkBoxBeanList.get(i).parentids) && checkBoxBeanList.get(i).parentids.equals(dataList.get(position).ids) && checkBoxBeanList.get(i).value.equals("1")) {
                                        //选中了有 显示
                                        isYesChecked = true;
                                    }
                                }
                            }
                        }
                    }
                }
                //当选择无时，大项隐藏
                if (!isYesChecked) {
                    holder.bg.setVisibility(View.GONE);
                } else {
                    holder.bg.setVisibility(View.VISIBLE);
                }
            } else {
                //flag不等于1的都显示
                holder.bg.setVisibility(View.VISIBLE);
            }


            //图标
            switch (dataList.get(position).ord) {
                case 4://环境保护基础资料
                    holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_huanjingbaohujichuziliao);
                    break;
                case 5://废水
                    holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_feishui);
                    break;
                case 6://废气
                    holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_feiqi);
                    break;
                case 90://危废
                    holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_weifei);
                    break;
                case 98://噪音
                    holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_zaoyin);
                    break;
                default:
                    holder.ivTijiaoshujuIcon.setImageResource(R.mipmap.ic_shenhe_huanjingbaohujichuziliao);
                    break;
            }

            //标题
            if (!TextUtils.isEmpty(dataList.get(position).module)) {
                holder.tvTijiaoshujuName.setText(dataList.get(position).module);
            } else {
                holder.tvTijiaoshujuName.setText("");
            }

            //更多显示与隐藏
            if (dataList.get(position).isShow) {
                holder.llTijiaoshujuMore.setVisibility(View.VISIBLE);
            } else {
                holder.llTijiaoshujuMore.setVisibility(View.GONE);
            }

            if (dataList.get(position).itemlist.size() <= 0) {
                holder.llTijiaoshujuMiddle.setVisibility(View.GONE);
            } else {
                holder.llTijiaoshujuMiddle.setVisibility(View.VISIBLE);
            }
            //显示与隐藏
            holder.ivTijiaoshujuMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (holder.llTijiaoshujuMore.getVisibility() == View.VISIBLE) {
                        holder.llTijiaoshujuMore.setVisibility(View.GONE);
                        holder.ivTijiaoshujuMore.setImageResource(R.mipmap.ic_down);
                        dataList.get(position).isShow = false;
                    } else {
                        holder.llTijiaoshujuMore.setVisibility(View.VISIBLE);
                        dataList.get(position).isShow = true;
                        if (dataList.get(position).itemlist.size() <= 0) {
                            holder.llTijiaoshujuMiddle.setVisibility(View.GONE);
                            holder.mylistview.setVisibility(View.GONE);
                        } else {
                            holder.llTijiaoshujuMiddle.setVisibility(View.VISIBLE);
                            holder.mylistview.setVisibility(View.VISIBLE);
                        }
                        holder.ivTijiaoshujuMore.setImageResource(R.mipmap.ic_up);
                    }
                }
            });

            //子列表
            BianJiZiLiaoItemAdapter adapter = new BianJiZiLiaoItemAdapter(context, dataList.get(position).itemlist);
            adapter.setMyClickListener(new BianJiZiLiaoItemAdapter.MyClickListener() {
                @Override
                public void onMyClick(String type, String ids, String value) {
                    myClickListener.onMyClick(type, ids, value);
                }
            });
            holder.mylistview.setAdapter(adapter);
        }
        return convertView;

    }

    /**
     * 单条刷新方法实践及原理解析
     *
     * @param listView 这个listview控件
     * @param ids      这条数据bean对应的ids
     */
    public void updateSingleRow(ListView listView, String ids) {
        if (listView != null) {
            int start = listView.getFirstVisiblePosition();
            LogUtils.e("start:" + start + "  last:" + listView.getLastVisiblePosition());
            for (int i = start, j = listView.getLastVisiblePosition(); i <= j; i++)
                if (!TextUtils.isEmpty(ids) && !TextUtils.isEmpty(((EnterpriseDetailReturn.DataBean.BasiclistBean) listView.getItemAtPosition(i)).ids) &&
                        ids.equals(((EnterpriseDetailReturn.DataBean.BasiclistBean) listView.getItemAtPosition(i)).ids)) {
                    View view = listView.getChildAt(i - start);
                    getView(i, view, listView);
                    break;
                }
        }
    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(String type, String ids, String value);
    }

    static class ViewHolder {
        @BindView(R.id.bg)
        LinearLayout bg;
        @BindView(R.id.iv_tijiaoshuju_icon)
        ImageView ivTijiaoshujuIcon;
        @BindView(R.id.tv_tijiaoshuju_name)
        TextView tvTijiaoshujuName;
        @BindView(R.id.iv_tijiaoshuju_more)
        ImageView ivTijiaoshujuMore;
        @BindView(R.id.mylistview)
        MyListView mylistview;
        @BindView(R.id.ll_tijiaoshuju_more)
        LinearLayout llTijiaoshujuMore;
        @BindView(R.id.ll_tijiaoshuju_middle)
        LinearLayout llTijiaoshujuMiddle;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
